 
* Withous scale invariance Cognitive

clear 
edit 
* Paste estimate point from excel
gen n=_n
gen m1=additive
* Model transformations
local i =1
local mod= "additive"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=(`mod') if n==8 //level 1 ELIMINATES THE 1.4

foreach num of numlist 9/20{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	//ELIMINATES THE 1.4
}
foreach num of numlist 21/31 {
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 38/45{
replace m`i'=((1.1^`mod')/((1.1^`mod')+1))*4 if n==`num'	 	//by 4
}

saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_level_add/point_estimates_add_pair_wo.dta", replace

clear 
edit
gen n=_n
gen m2 =multiplicative
local i =2
local mod= "multiplicative"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=1+(`mod') if n==8 //1+level 2 ELIMINATES THE 1.4

foreach num of numlist 9/19{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	//ELIMINATES THE 1.4
}
foreach num of numlist 20/30 37{
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 38/45{
replace m`i'=((1.1^`mod')/((1.1^`mod')+1))*4 if n==`num'	 	//by 4
}

saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_level_mul/point_estimates_mul_pair_wo.dta", replace
 
 * Additive Model
use "Y:\Fuyao_Erlfang\estimates\pair\cog_level_add/bootsresults.dta", clear 
 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=(var8) //ELIMINATES THE 1.4
foreach num of numlist 9/20{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//ELIMINATES THE 1.4	
}
foreach num of numlist 21/31 {
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 38/45{
gen var`num'_2=((1.1^var`num')/((1.1^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var31_2 var32-var37 var38_2-var45_2 var46-var60{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}
* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var25_2 var26_2 var31_2  {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<997
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}
foreach var of varlist  var31_2  {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<992
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}
keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 32/37 46/60{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/31 38/45{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "Y:\Fuyao_Erlfang\estimates\pair\cog_level_add/point_estimates_add_pair_wo.dta"
gen out= 1 if m1 > var_ub_ | m1 <var_lb_
br 
drop out 
tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f)
gen t = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t = "(" + var_sd_ + ")" if q==2
tostring m1 , replace force format(%6.3f)
replace t=m1  if q ==1

keep q n var1 t
order q n var1 t
replace var1="" if q==2| q==3
saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_level_add/tempc_add_pair_wo.dta" , replace
gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t 
drop q n
texsave using "Y:\Fuyao_Erlfang\estimates\pair\cog_level_add/Cog_add_pair_wo.tex", width(\raggedleft) ///
    replace size(scriptsize) align(ll|c) location(ht) varlabels frag ///
    title(Cognitive Additive) marker(tab:Cog_)  ///
    footnote("Note:", size(scriptsize)) autonumber


* Multiplicative
use "Y:\Fuyao_Erlfang\estimates\pair\cog_level_mul/bootsresults.dta", clear
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8) //ELIMINATES THE 1.4
foreach num of numlist 9/19{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//ELIMINATES THE 1.4	
}
foreach num of numlist 20/30 37{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 38/45{
gen var`num'_2=((1.1^var`num')/((1.1^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var30_2 var31-var36 var37_2-var45_2 var46-var60{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}

* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var26_2 var28_2 var30_2{
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<997
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}

keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 31/36 46/60{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/30 37/45{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q

rename numero n
merge m:1 n using "Y:\Fuyao_Erlfang\estimates\pair\cog_level_mul/point_estimates_mul_pair_wo.dta"
gen out= 1 if m2 > var_ub_ | m2 <var_lb_
br 
drop out 
tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f)
gen t2 = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t2= "(" + var_sd_ + ")" if q==2

tostring  m2, replace force format(%6.3f)
replace t2=m2 if q ==1
keep q n var1 t2
order q n var1 t2
replace var1="" if q==2| q==3
saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_level_mul/tempc_multi_pair_wo.dta", replace 

gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t2

drop q n  
*paste manually the levels in a new column
texsave using "Y:\Fuyao_Erlfang\estimates\pair\cog_level_mul/Cog_Mult_pair_wo.tex", width(\raggedleft) ///
    replace size(scriptsize) align(llcllc) location(ht) varlabels frag ///
    title(Language Multiplicative) marker(tab:lang_)  ///
    footnote("Note:", size(scriptsize)) autonumber


